package com.example.fieldnotes;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;

@SuppressWarnings("unused")
public class Save extends Activity {
	private WritableWorkbook workbook;
	private File sdCard, directory;
	private File file;
	private WritableSheet sheet;
	private WorkbookSettings wbSettings;
	private String fNameXls = "", name = "testfile.xls";
	private int current = 0, choice = 0, x = 0, y = 0;
	private dataStuff data;
	private SharedPreferences pref;
	
	public Save(int choice, String newFileName, SharedPreferences pref)
	{
		fNameXls = newFileName;
		
        this.pref = pref;
        x = pref.getInt("eRow", 10);
        y = pref.getInt("eRange", 5);
		
		if (choice == 0 && fNameXls.matches(""))
			fNameXls = "notes.xls";
		else if (choice == 1 && fNameXls.matches(""))
			fNameXls = "rating.xls";
		else if (choice == 2 && fNameXls.matches(""))
			fNameXls = "dates.xls";
		else if (choice == 3 && fNameXls.matches(""))
			fNameXls = "";
		
		current++;
		this.choice = choice;
		
		//write pref
		Editor edit = pref.edit();
		edit.putString("stringIn", fNameXls);
		edit.putString("previous", "");
		edit.commit();
		
	    sdCard = Environment.getExternalStorageDirectory();
	    directory = new File (sdCard.getAbsolutePath() + "/MAGICStuff");
	    directory.mkdir();
	    
	    file = new File(directory, fNameXls);
		
		wbSettings = new WorkbookSettings();
		wbSettings.setLocale(Locale.US);
		  
		try {
			
		}
		catch (Exception e)
		{
			
		}
		      
	}
	
	public int add(int x, int y, dataStuff data)
	{
		this.data = data;
		Runnable thread = new MyThread(this.data, file, directory, fNameXls, workbook, wbSettings, sheet, this.x + 1, this.y);
		new Thread(thread).start();
		
		return 0;
	}
	
	public void newSaveName(String name)
	{
		fNameXls = name;
	}
}

class MyThread implements Runnable {
	private dataStuff data;
	private File file, directory;
	private String fNameXls;
	private WritableWorkbook workbook;
	private WorkbookSettings wbSettings;
	private WritableSheet sheet;
	private int x, y;
	
	public MyThread(dataStuff data, File file, File directory, String fNameXls, WritableWorkbook workbook, WorkbookSettings wbSettings, WritableSheet sheet, int x, int y) {
		this.data = data;
		this.file = file;
		this.directory = directory;
		this.fNameXls = fNameXls;
		this.workbook = workbook;
		this.wbSettings = wbSettings;
		this.sheet = sheet;
		this.x = x;
		this.y = y;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			file = new File(directory, fNameXls);
			workbook = Workbook.createWorkbook(file, wbSettings);
		    sheet = workbook.createSheet("First Sheet", 0);
		    
		    for (int i = 0; i < this.y; i++)
		    {
		    	for (int j = 0; j < this.x; j++)
		    	{
		    		try {
		    			Label temp = new Label(i, j, data.get(i, j));
		    			sheet.addCell(temp);
		    		} catch (ArrayIndexOutOfBoundsException e) { break; }
		    		////System.out.print(data.get(i, j));
		    	}
		    	////System.out.println();
		    }
		    
    		workbook.write();
			workbook.close();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//return -1;
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//return -1;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//return -1;
		}
	}
}
